package com.camera.yl.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.camera.yl.R;
import com.camera.yl.bean.MenuItemInfo;
import com.camera.yl.widght.GridItemView;

import java.util.List;

public class SubMenuAdapter extends RecyclerView.Adapter<SubMenuAdapter.ViewHolder> {
    private final String TAG = SubMenuAdapter.class.getSimpleName();
    private List<MenuItemInfo> subMenuList;
    private Context mContext;

    public SubMenuAdapter(Context mContext, List<MenuItemInfo> list) {
        this.mContext = mContext;
        this.subMenuList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sub_menu,parent,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MenuItemInfo subMenu = subMenuList.get(position);
        if (subMenu.iconResId == 0) {
            holder.gridItemView.setIconDrawable(subMenu.iconDrawable);
//            holder.ivPictreu.setImageDrawable(subMenu.iconDrawable);
        }else{
            holder.gridItemView.setIcon(subMenu.iconResId);
//            holder.ivPictreu.setImageResource(subMenu.iconResId);
        }
        holder.gridItemView.setText(subMenu.name);
//        holder.tvNmae.setText(subMenu.name);
    }

    @Override
    public int getItemCount() {
        return subMenuList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//        public TextView tvNmae;
//        private ImageView ivPictreu;
        public GridItemView gridItemView;
        public ViewHolder(View view){
            super(view);
//            tvNmae = (TextView) view.findViewById(R.id.tv_name);
//            ivPictreu = (ImageView) view.findViewById(R.id.iv_picture);
            gridItemView = view.findViewById(R.id.grid_item);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (clickListener != null) {
                clickListener.onClick(itemView, getAdapterPosition());
            }
        }
    }

    private OnItemClickListener clickListener;

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);
    }
}
